﻿using System.Linq.Expressions;
using Dyao.Book.Contract;
using Dyao.Book.Entity;
using Dyao.Book.Repository.Abstractions;

namespace Dyao.Book.Repository.Repositories
{
    using Foundation.Replacement;

    public class BookCatalogueMatchSourceRepository : DataRepository<BookCatalogueMatchSourceEntity>, IBookCatalogueMatchSourceRepository
    {
        /// <inheritdoc cref="IBookCatalogueMatchSourceRepository.BulkInsertAsync"/>
        public new Task BulkInsertAsync(IEnumerable<BookCatalogueMatchSourceEntity> entities, CancellationToken cancellationToken)
        {
            return base.BulkInsertAsync(entities, cancellationToken);
        }

        /// <inheritdoc cref="IBookCatalogueMatchSourceRepository.BulkUpdateAsync"/>
        public Task BulkUpdateAsync(IEnumerable<BookCatalogueMatchSourceEntity> entities, Expression<Func<BookCatalogueMatchSourceEntity, object>> updateSelector, CancellationToken cancellationToken = default)
        {
            return base.BulkUpdateAsync(entities, op =>
            {
                op.ColumnPrimaryKeyExpression = key => new { key.ID };
                op.IncludeOnUpdateExpression = updateSelector;
            }, cancellationToken);
        }

        /// <inheritdoc cref="IBookCatalogueMatchSourceRepository.GetListByFilterAsync"/>
        public async Task<List<T>> GetListByFilterAsync<T>(ChapterSourceFilterDto filter, Expression<Func<BookCatalogueMatchSourceEntity, T>> selector, CancellationToken cancellationToken = default)
        {
            var query = this.GetQueryable()
                .Where(bm => bm.SourceKey == filter.SourceKey)
                .Where(bm => bm.BookId == filter.BookId)
                .Select(selector);

            return await query.ToListAsync(cancellationToken);
        }
    }
}
